import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        # 'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        # list = []
        # for quote in response.css('div.quote'):
        #     author = quote.css('small.author::text').get()
        #     text = quote.css('span.text::text').get()
        #     list.append(author)
        # filename = f'quotes.txt'
        # with open(filename, 'w') as f:
        #     f.write(str(list))
        next_page = response.xpath("//li[@class='next']/a/@href").get()
        title = response.xpath("//title").get()
        filename = f'quotes.txt'
        with open(filename, 'a') as f:
            f.write(str(title))
        # if next_page is not None:
        #     next_page = response.urljoin(next_page)
        #     yield scrapy.Request(next_page, callback=self.parse)
        if next_page is not None:
            yield response.follow(next_page, callback=self.parse)
        self.log(next_page)
